{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Net(\n",
      "  (conv1): Conv2d(1, 10, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
      "  (conv2): Conv2d(10, 20, kernel_size=(3, 3), stride=(1, 1))\n",
      "  (fc1): Linear(in_features=500, out_features=10, bias=True)\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "# define cnn model\n",
    "\n",
    "class Net(nn.Module):\n",
    "\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        \n",
    "        # 1 input image channel (grayscale), 10 output channels/feature maps\n",
    "        # 3x3 square convolution kernel\n",
    "        ## output size = (W-F)/S +1 = (28-3)/1 +1 = 26\n",
    "        # the output Tensor for one image, will have the dimensions: (10, 26, 26)\n",
    "        # after one pool layer, this becomes (10, 13, 13)\n",
    "        self.conv1 = nn.Conv2d(1, 10, 3)\n",
    "        \n",
    "        # maxpool layer\n",
    "        # pool with kernel_size=2, stride=2\n",
    "        self.pool = nn.MaxPool2d(2, 2)\n",
    "        \n",
    "        # second conv layer: 10 inputs, 20 outputs, 3x3 conv\n",
    "        ## output size = (W-F)/S +1 = (13-3)/1 +1 = 11\n",
    "        # the output tensor will have dimensions: (20, 11, 11)\n",
    "        # after another pool layer this becomes (20, 5, 5); 5.5 is rounded down\n",
    "        self.conv2 = nn.Conv2d(10, 20, 3)\n",
    "        \n",
    "        # 20 outputs * the 5*5 filtered/pooled map size\n",
    "        # 10 output channels (for the 10 classes)\n",
    "        self.fc1 = nn.Linear(20*5*5, 10)\n",
    "        \n",
    "\n",
    "    # define the feedforward behavior\n",
    "    def forward(self, x):\n",
    "        # two conv/relu + pool layers\n",
    "        x = self.pool(F.relu(self.conv1(x)))\n",
    "        x = self.pool(F.relu(self.conv2(x)))\n",
    "\n",
    "        # prep for linear layer\n",
    "        # flatten the inputs into a vector\n",
    "        x = x.view(x.size(0), -1)\n",
    "        \n",
    "        # one linear layer\n",
    "        x = self.fc1(x)\n",
    "        # a softmax layer to convert the 10 outputs into a distribution of class scores\n",
    "        x = F.log_softmax(x, dim=1)\n",
    "        \n",
    "        # final output\n",
    "        return x\n",
    "\n",
    "# instantiate and print your Net\n",
    "net = Net()\n",
    "print(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
